package com.apps.interceptor;


import com.alibaba.fastjson.JSON;
import com.apps.common.model.po.ApiResult;
import com.apps.globals.advent.service.IKupaAdventService;
import com.apps.globals.foot.service.IKupaFooterColumnService;
import com.apps.globals.global.service.IKupaGlobalService;
import com.apps.goods.service.IKupaGoodsCatService;
import com.apps.helper.PageConextHelper;
import com.apps.helper.ThreadContextHelper;
import com.apps.helper.UserConextHelper;
import com.apps.member.model.po.KupaMember;
import com.apps.sutils.StringUtils;
import com.apps.toolkit.KupaRequestResponseToolkit;
import com.apps.vip.service.IKupaVipGoodsService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 所有非静态请求拦截器，主要干两个事
 * 1、将所有请求、响应设置到当前线程中,便于随时获取JavaEE对象
 * 2、拦截会员需要登录的请求
 *
 * @author: YuXD
 * @create: 2019-05-20 11:51
 **/
@Data
@Slf4j
//@Component
public class GlobalInterceptor implements HandlerInterceptor {

    private IKupaFooterColumnService footerColumnService;

    private IKupaGlobalService globalService;

    private IKupaAdventService adventService;

    private IKupaGoodsCatService goodsCatService;

    private IKupaVipGoodsService vipGoodsService;

    //登录才能调用的接口
    private String[] checkIsMemberLoginApiPathPatterns = {"/api/member/**"};
    //登录才能看的页面接口
    private String[] checkIsMemberLoginPagePathPatterns = {"/page/member/**"};


    public GlobalInterceptor(IKupaFooterColumnService footerColumnService,
                             IKupaGlobalService globalService,
                             IKupaAdventService adventService,
                             IKupaGoodsCatService catService,
                             IKupaVipGoodsService vipGoodsServices)
    {
        this.footerColumnService = footerColumnService;
        this.globalService = globalService;
        this.adventService = adventService;
        this.goodsCatService = catService;
        this.vipGoodsService = vipGoodsServices;
    }

    /**
     * 前置拦截器
     * @param request
     * @param response
     * @param o
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception
    {
        // 将所有请求、响应设置到当前线程中
        ThreadContextHelper.setLocal(request, response);
        // 对处于需要进行登录校验的访问请求进行登录校验
        String url = request.getRequestURI();
        KupaMember member= UserConextHelper.getCurrentMember();

        //如果是必须登录的接口则返回调用失败
        if(StringUtils.isContains(checkIsMemberLoginApiPathPatterns,url) && null == member)
        {
            KupaRequestResponseToolkit.responseWrite(JSON.toJSONString(new ApiResult<>().dataError(10000,"请先登录")), response);
            return false;
        }
        //若访问必须登录的页面且没登录导到登录页面
        if(StringUtils.isContains(checkIsMemberLoginPagePathPatterns,url) && null == member)
        {
            response.sendRedirect("/thirdparty/login.html");
            return false;
        }
        //把用户塞进页面
        if(null != member)
           request.setAttribute("member", member);
        else
           request.setAttribute("member",null);
        //全局参数和页脚参数放进所有页面
        request.setAttribute("footer", footerColumnService.QueryMap());
        request.setAttribute("global", globalService.QueryMap());
        request.setAttribute("advent", adventService.QueryMap());
        request.setAttribute("goodscat",goodsCatService.getMasterData());
        request.setAttribute("buylist",vipGoodsService.queryByActivity());
        //设置页面路径管理
        PageConextHelper.SetRequest(request);
        return true;
    }


    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) {
        return;
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) {
        /*
        从当前请求线程中移除请求、响应、Session对象
         */
        ThreadContextHelper.remove();
    }


}
